package org.example.engine;

import com.lmax.disruptor.EventHandler;
import io.netty.util.collection.IntObjectHashMap;
import org.example.command.CmdResultCode;
import org.example.model.OrderBook;

import static org.example.order.CmdType.NEW_ORDER;

public class MatchHandler implements EventHandler<RbCmd> {

    private IntObjectHashMap<OrderBook> orderBookMap ;

    public MatchHandler(IntObjectHashMap orderBookMap) {
        this.orderBookMap = orderBookMap;
    }

    private CmdResultCode processCmd(RbCmd cmd) {
        switch (cmd.command) {
            case NEW_ORDER:
                return orderBookMap.get(cmd.code).newOrder(cmd);
            default:
                return CmdResultCode.RISK_INVALID_CODE;
        }
    }

    @Override
    public void onEvent(RbCmd rbCmd, long l, boolean b) throws Exception {
        System.out.println("match handler ..................");
        rbCmd.resultCode = processCmd(rbCmd);
    }
}
